package nl.rainbowsheep.reisorg.rest.service.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import nl.rainbowsheep.reisorg.rest.service.util.Localizer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name = "accommodations")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class Accommodation implements Serializable, ILocalized {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "accommodation_id")
    private Long id;

    @Column(name = "accommodation_type_fk")
    private Long accommodationTypeId;

    @Column(name = "fake_description")
    private String description;

    @Column(name = "last_update")
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:SS")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date lastUpdate;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "accommodations_locale", joinColumns = @JoinColumn(name = "accommodations_fk"))
    @MapKeyJoinColumn(name = "languages_fk")
    private Map<Language, AccommodationLocalized> locale;

    @Column(name = "city_fk")
    private Long cityId;

    @Column(name = "country_fk")
    private Long countryId;

    @Column(name = "title")
    @NotNull
    private String title;

    @Column(name = "street")
    @NotNull
    private String street;

    @Column(name = "housenumber")
    @NotNull
    private String housenumber;

    @Column(name = "postalcode")
    @NotNull
    private String postalCode;

    @Column(name = "is_active")
    @NotNull
    private Boolean isActive;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonIgnore
    public Map getLocale() {
        return locale;
    }

    public void setLocale(Map locale) {
        this.locale = locale;
    }
    
    public Long getAccommodationTypeId() {
        return accommodationTypeId;
    }

    public void setAccommodationTypeId(Long accommodationTypeId) {
        this.accommodationTypeId = accommodationTypeId;
    }

    public Long getCityId() {
        return cityId;
    }

    public void setCityId(Long cityId) {
        this.cityId = cityId;
    }

    public Long getCountryId() {
        return countryId;
    }

    public void setCountryId(Long countryId) {
        this.countryId = countryId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String Street) {
        this.street = Street;
    }

    public String getHousenumber() {
        return housenumber;
    }

    public void setHousenumber(String housenumber) {
        this.housenumber = housenumber;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public Boolean isIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {

        this.description = description;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    @Override
    @PostLoad
    @PostUpdate
    public void localize() {
        AccommodationLocalized localizedObject = (AccommodationLocalized) Localizer.getLocalizedObject(this.locale, AccommodationLocalized.class);
        this.description = localizedObject.getDescription();
    }

    @Override
    @PreUpdate
    @PrePersist
    public void parseLocalizedData() {
        AccommodationLocalized localizedObject = null;
        if (this.locale == null) {
            this.locale = new HashMap<Language, AccommodationLocalized>();
        }
        if (Localizer.getLocalizedObject(this.locale, AccommodationLocalized.class) != null) {
            localizedObject = (AccommodationLocalized) Localizer.getLocalizedObject(this.locale, AccommodationLocalized.class);
        } else {
            localizedObject = new AccommodationLocalized();
            this.locale.put(Localizer.getCurrentLanguage(), localizedObject);
        }
        localizedObject.setDescription(this.description);
    }

}
